<?php
namespace app\manage\controller;
use app\manage\controller\BaseController;
use think\facade\Db;
use think\facade\View;
use app\manage\model\Cate as Catemodel;
class Cate extends BaseController
{
    public function index()
    {
		$key=input('key','');
        View::assign('key',$key);
    	if(request()->isPost()){
    		$data=input('post.');
			$datasort=Catemodel::where('catename','like','%'.$data['key'].'%')->order('id','ASC')->select();
            View::assign('datasort',$datasort);
    	}else{
    		$cate=new Catemodel();
			$datasort=$cate->tree();
            View::assign('datasort',$datasort);
    	}
		$count1=Db::name('cate')->count();
        View::assign('count1', $count1);
       	return View::fetch();
    }
	public function ajax()
    {
    	$data=input('post.');
		if($data['type']=='cate_sort'){
			$arrlength=count($data['id']);
			$ar=[];
			for($x=0;$x<$arrlength;$x++)
			{
			    $ar[$x]=['id'=>$data['id'][$x], 'sort'=>$data['sort'][$x]];
			}
			$cate=new Catemodel();
			$info=$cate->saveAll($ar);
			if($info){
                return json(['code' => 1, 'message' => '操作成功']);
            } else {
                return json(['code' => 0, 'message' => '操作失败']);
            }
		}
		if($data['type']=='cate_del'){
			if(Db::name('cate')->where('fid',$data['id'])->find()){
                return json(['code' => 2, 'message' => '拥有下一级']);
			}else{
				//删除图片(单页内容图片)
				if(Catemodel::destroy($data['id'])){
                    return json(['code' => 1, 'message' => '操作成功']);
                } else {
                    return json(['code' => 0, 'message' => '操作失败']);
                }
			}
			
		}
		if($data['type']=='cate_start'){
			if(Db::name('cate')->where('id',$data['id'])->update(['isopen'=>1])){
                return json(['code' => 1, 'message' => '操作成功']);
            } else {
                return json(['code' => 0, 'message' => '操作失败']);
            }
		}
		if($data['type']=='cate_stop'){
			if(Db::name('cate')->where('id',$data['id'])->update(['isopen'=>0])){
                return json(['code' => 1, 'message' => '操作成功']);
            } else {
                return json(['code' => 0, 'message' => '操作失败']);
            }
		}
        return json(['code' => 0, 'message' => '失败']);
    }
	public function add()
    {
    	if(request()->isPost()){
			$data=input('post.');
			
			$validate = new \app\manage\validate\Cate;
			if(!$validate->check($data)){
				$this->error($validate->getError());
			}
            $file = request()->file('');
            if (isset($file['pic'])) {
                try {
                    validate(['pic'=>'fileSize:5242880|fileExt:jpg,png,gif,webp,jpeg'])->check($file);
                    $info=\think\facade\Filesystem::disk('public')->putFile( 'cate', $file['pic']);
                    $data['pic']='/storage/'.strtr($info, " \ ", " / ");
                } catch (\think\exception\ValidateException $e) {
                    $this->error($e->getMessage());
                }
            }
			$user = new Catemodel($data);
			$res=$user->save();
			if($res){
				$this->success('添加成功',url('cate/index',['st'=>1]));
			}else{
				$this->error('栏目添加失败了');
			}
    	}
		$cate=new Catemodel();
		$datasort=$cate->tree();
        View::assign('datasort',$datasort);
		
		$dir=base_path().'index/view/index';
		$wo=scandir($dir);
		$html=[];
		foreach($wo as $k=>$v){
			if(\is_file($dir.'/'.$v) and substr($v,0,2)!='m_'){
				$html[]=[
					'name'=>$v,
				];
			}
		}
        View::assign('html',array_reverse($html));
		
       return View::fetch();
    }
	public function edit()
    {
    	if(request()->isPost()){
			$data=input('post.');
			$validate = new \app\manage\validate\Cate;
			if(!$validate->scene('edit')->check($data)){
				$this->error($validate->getError());
			}	
			if($data['id']==$data['fid']){
				$this->error('自己不能成为自己的下一级！');
			}
            $file = request()->file('');
            if (isset($file['pic'])) {
                try {
                    validate(['pic'=>'fileSize:5242880|fileExt:jpg,png,gif,webp,jpeg'])->check($file);
                    $info=\think\facade\Filesystem::disk('public')->putFile( 'cate', $file['pic']);
                    $data['pic']='/storage/'.strtr($info, " \ ", " / ");
                } catch (\think\exception\ValidateException $e) {
                    $this->error($e->getMessage());
                }
            }
    		$user = new Catemodel();
			$res=$user->update($data,['id' => input('id')]);
			if($res){
				return $this->success('修改成功',url('cate/index',['st'=>1]));
			}else{
				$this->error('栏目修改失败了');
			}
    	}
		
		$cid=input('id');
		$data=Db::name('cate')->where('id',$cid)->find();
        View::assign('data',$data);
		$cate=new Catemodel();
		$datasort=$cate->tree();
        View::assign('datasort',$datasort);
		$dir=base_path().'index/view/index';
		$wo=scandir($dir);
		$html=[];
		foreach($wo as $k=>$v){
			if(\is_file($dir.'/'.$v) and substr($v,0,2)!='m_'){
				$html[]=[
					'name'=>$v,
				];
			}
		}
        View::assign('html',array_reverse($html));
       return View::fetch();
    }
}
